package cn.dwj.hadoop.mr.areapartition;

import org.apache.hadoop.mapreduce.Partitioner;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/12/27 0027.
 */
public class AreaPartitioner<KEY, VALUE> extends Partitioner<KEY, VALUE> {

    private static HashMap<String, Integer> areaMap = new HashMap<String, Integer>();
    static {
        areaMap.put("135", 0);
        areaMap.put("136", 1);
        areaMap.put("137", 2);
        areaMap.put("138", 3);
        areaMap.put("139", 4);
        System.out.println(areaMap);
    }
    public int getPartition(KEY key, VALUE value, int i) {
        System.out.println(String.format("key=%s, value=%s, i=%d", key, value, i));
        //从key中拿到手机号，查询手机归属地字典，不同的省份返回不同组号
        String str = key.toString().substring(0, 3);
        Integer code = areaMap.get(str);
        int areaCode = null == code? 5 : code;
        System.out.println(key.toString() + "--"+ areaCode);
        return areaCode;
    }
}
